package com.example.sociamedia.post.controller;

import com.example.sociamedia.post.dto.CommentRequest;
import com.example.sociamedia.post.dto.CommentResponse;
import com.example.sociamedia.post.entity.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/comments")
public class CommentController {
    private final CommentService commentService;

    @Autowired
    public CommentController(CommentService commentService) {
        this.commentService = commentService;
    }

    @PostMapping("")
    public ResponseEntity<CommentResponse> addComment(@RequestHeader("Authorization") String sessionToken, @RequestBody CommentRequest request) {
        CommentResponse resp = commentService.addComment(sessionToken, request);
        return new ResponseEntity<>(resp, resp.isSuccess() ? HttpStatus.OK : HttpStatus.BAD_REQUEST);
    }

    @GetMapping("/{postId}")
    public ResponseEntity<List<Comment>> getCommentsByPost(@PathVariable Long postId) {
        return ResponseEntity.ok(commentService.getCommentsByPost(postId));
    }

    @DeleteMapping("/{commentId}")
    public ResponseEntity<CommentResponse> deleteComment(@RequestHeader("Authorization") String sessionToken, @PathVariable Long commentId) {
        CommentResponse resp = commentService.deleteComment(sessionToken, commentId);
        return new ResponseEntity<>(resp, resp.isSuccess() ? HttpStatus.OK : HttpStatus.BAD_REQUEST);
    }
} 